package com.zmn.uac.api.controllers.oauth.login;

import com.zmn.common.dto2.ResponseDTO;
import com.zmn.uac.api.constant.HeaderParamKeyConst;
import com.zmn.uac.business.interfaces.oauth.login.ByteDanceLoginBService;
import com.zmn.uac.common.dict.ErrorCodeDict;
import com.zmn.uac.common.dio.oauth.login.UserAuthLoginDIO;
import com.zmn.uac.common.dro.oauth.login.UserAuthLoginDRO;
import com.zmn.uac.common.info.bytebeat.ByteBeatAuthPhoneInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * 类描述：字节跳动、抖音授权登陆
 *
 * @author xujie
 * @since 2021/06/11 18:30
 */
@Api(tags = "字节跳动、抖音授权登陆")
@Slf4j
@RestController
@RequestMapping("/byteBeat")
public class ByteDanceLoginController extends LoginBaseController {

    private static final String TAG = "字节跳动、抖音授权登陆";

    @Resource
    ByteDanceLoginBService byteDanceLoginBService;

    /**
     * 字节跳动、抖音授权登陆
     *
     * @return ResponseDTO<UserAuthLoginDRO>
     * @author xujie
     * @since 2021/06/11
     */
    @ApiOperation("小程序手机号授权登录")
    @PostMapping(value = "/login",produces = MediaType.APPLICATION_JSON_VALUE,
            headers = {
                    HeaderParamKeyConst.PLAT_ID_KEY,
                    HeaderParamKeyConst.PROVINCE_ID_KEY,
                    HeaderParamKeyConst.CITY_ID_KEY,
                    HeaderParamKeyConst.PROVINCE_NAME_KEY,
                    HeaderParamKeyConst.CITY_NAME_KEY,
                    HeaderParamKeyConst.CHANNEL_ID_KEY,
            })
    public ResponseDTO<UserAuthLoginDRO> login(HttpServletRequest request, @RequestBody ByteBeatAuthPhoneInfo byteBeatAuthPhoneInfo) {

        UserAuthLoginDIO dio = getUserAuthLogin(request, byteBeatAuthPhoneInfo.getCode(), byteBeatAuthPhoneInfo.getEntrance());
        String uuid = dio.getUuid();
        log.info("[{}], {}, 字节跳动、抖音授权登陆调用参数: {}", TAG, uuid, dio);
        if (Objects.isNull(dio.getChannelId()) && Objects.isNull(dio.getShopId())) {
            return ResponseDTO.fail(ErrorCodeDict.ERROR_CODE_PARAM_NULL, "渠道ID,店鋪ID不能同时为空!");
        }
        ResponseDTO<UserAuthLoginDRO> responseDTO = byteDanceLoginBService.login(dio, byteBeatAuthPhoneInfo);
        log.info("[{}], {}, 字节跳动、抖音授权登陆调用响应: {}", TAG, uuid, responseDTO);
        UserAuthLoginDRO dro = responseDTO.getData();
        if (Objects.nonNull(dro)) {
            log.info("[{}], {}, 字节跳动、抖音授权登陆成功，响应UserAuthLoginDRO:{}", TAG, uuid, dro);
            return ResponseDTO.success(dro);
        }
        log.error("[{}], {}, 字节跳动、抖音授权登陆失败，msg:{}", TAG, uuid, responseDTO.getMessage());
        return ResponseDTO.fail(responseDTO.getMessage());
    }
}
